package com.example.actearly_revised;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import com.example.actearly_revised.QuestionsObject.Node;

import android.content.Context;

public class Creating_Node_List {
	Context newContext;
	int count;
	public static HashMap<Integer, Integer> milestoneYearMap = new HashMap<Integer, Integer>();
	static Map<Integer, LinkedList<Question_Node>> milestone_map = new HashMap<Integer, LinkedList<Question_Node>>();
	SQLogic conn;
	public Creating_Node_List(int milestone, Context con){
		this.newContext = con;
		int[] ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};
		milestone_map.put(milestone, new LinkedList<Question_Node>());
		for(int i = 0; i < ageID.length; i++){
			milestoneYearMap.put(ageID[i], 0);
		}
	}
	
	public void increment(int milestone){
		int temp = milestoneYearMap.get(milestone);
		milestoneYearMap.remove(milestone);
		temp++;
		milestoneYearMap.put(milestone, temp);
	}

	public void decrement(int milestone){
		int temp = milestoneYearMap.get(milestone);
		milestoneYearMap.remove(milestone);
		temp--;
		milestoneYearMap.put(milestone, temp);
	}
	
	public int getCountOfMilestone(int milestone){
		System.out.println(milestone);
		int temp = milestoneYearMap.get(milestone);
		return temp;
	}
	
	public void add(int milestone){
		int count = 0;
		milestone_map.put(milestone, new LinkedList<Question_Node>());
		conn = new SQLogic(newContext);
		Change_of_Answer_Database.setConnection(newContext);
		conn.open();
		QuestionsObject qd =  conn.RetrieveAllQuestions(milestone ,0);
		System.out.println(qd.newMap.keySet());
		//Iterator<String> newIterator = qd.getMap().keySet().iterator();
		Collection<Node> c = qd.getMap().values();
		Iterator<Node> newIterator = c.iterator();
		while(newIterator.hasNext()){
			//Iterator<String> temp = newIterator;
			Node temp1 = newIterator.next();
			//Question_Node tempNode = new Question_Node(qd.getMap().get(temp1).Question, 0, qd.getMap().get(temp1).q_id);
			Question_Node tempNode = new Question_Node((temp1).Question, 0, (temp1).q_id);
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>"+tempNode.question +"<<<<<<<<<<<<<<<<<");
			if(!milestone_map.get(milestone).contains(tempNode)){
			milestone_map.get(milestone).add(tempNode);
			
			System.out.println(milestone_map.get(milestone).get(count).question);
			count++;
			}
		}
		conn.close();
	}

	public static void change(int milestone, String question, int answer, Context context, int profile_number){
		System.out.println(question);
		int temp = 0;
		System.out.println("????????????" + answer);
		System.out.println(milestone_map.get(milestone));
		for(int j = 0; j < milestone_map.get(milestone).size(); j++){
			if(milestone_map.get(milestone).get(j).getQuestion().equals(question)){
				temp = j;
			}
		}
		System.out.println(temp);

			milestone_map.get(milestone).get(temp).changeAnswer(answer, context, profile_number);
		
		//milestone_map.get(milestone).get(temp).changeAnswer(answer);
		//milestone_map.get(milestone).get(temp).flag_answered = true;
	}
	
	public static int gettingLastEditedAnswer(int milestone){
		LinkedList<Question_Node> tempList = milestone_map.get(milestone);
		System.out.println(tempList);
		for(int i =0; i < tempList.size(); i++){
			System.out.println(tempList.get(i).getAnswer());
			if(tempList.get(i).flag_answered == false){
				return i;
			}
		}
		return tempList.size()-1;
	}


}
